package com.wan.controller;

import com.wan.pojo.Menu;
import com.wan.pojo.Page;
import com.wan.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//菜单管理控制器
@Controller
@RequestMapping("/admin/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    /*
登录成功后的欢迎页
* */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.addObject("topList", menuService.findTopList());
        model.setViewName("menu/list");
        return model;
    }
//    获取菜单列表，包含搜索框模糊查询功能
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getMenuList(Page page,@RequestParam(name ="name",required = false,defaultValue ="") String name){
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("offset",page.getOffset());
        queryMap.put("pageSize",page.getRows());
        queryMap.put("name",name);
        List<Menu> menuList = menuService.getMenuList(queryMap);
        map.put("rows",menuList);
        map.put("total",menuService.getTotal(queryMap));
        return map;
    }
    /**
     * 获取指定目录下的系统icon集合
     * @param request
     * @return
     */
    @RequestMapping(value="/getIcons",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getIconList(HttpServletRequest request){
        Map<String, Object> ret = new HashMap<String, Object>();
        String realPath = request.getSession().getServletContext().getRealPath("/");
        File file = new File(realPath + "\\resources\\admin\\easyui\\css\\icons");
        List<String> icons = new ArrayList<String>();
        if(!file.exists()){
            ret.put("type", "error");
            ret.put("msg", "文件目录不存在！");
            return ret;
        }
        File[] listFiles = file.listFiles();
        for(File f:listFiles){
            if(f!= null && f.getName().contains("png")){
                icons.add("icon-" + f.getName().substring(0, f.getName().indexOf(".")).replace("_", "-"));
            }
        }
        ret.put("type", "success");
        ret.put("content", icons);
        return ret;
    }
    /*菜单添加*/
    @RequestMapping(value = "/add",method =RequestMethod.POST )
    @ResponseBody
    public Map<String,String> addMenu(Menu menu){
        Map<String, String> map = new HashMap<String, String>();
        if(menu==null){
            map.put("type","error");
            map.put("msg","请填写正确的菜单信息!");
            return map;
        }
        if(StringUtils.isEmpty(menu.getName())){
            map.put("type","error");
            map.put("msg","请填写菜单名称!");
            return map;
        }
        if(StringUtils.isEmpty(menu.getIcon())){
            map.put("type","error");
            map.put("msg","请填写菜单图标!");
            return map;
        }
        if(menu.getParentId() == null){
            menu.setParentId(0l);
        }
        if(menuService.addMenu(menu) <= 0){
            map.put("type", "error");
            map.put("msg", "添加失败，请联系管理员!");
            return map;
        }
        map.put("type","success");
        map.put("msg","添加成功！");
        return map;
    }
    /*菜单修改*/
    @RequestMapping(value = "/edit",method =RequestMethod.POST )
    @ResponseBody
    public Map<String,String> editMenu(Menu menu){
        Map<String, String> map = new HashMap<String, String>();
        if(menu==null){
            map.put("type","error");
            map.put("msg","请选择正确的菜单信息!");
            return map;
        }
        if(StringUtils.isEmpty(menu.getName())){
            map.put("type","error");
            map.put("msg","请填写菜单名称!");
            return map;
        }
        if(StringUtils.isEmpty(menu.getIcon())){
            map.put("type","error");
            map.put("msg","请填写菜单图标!");
            return map;
        }
        if(menu.getParentId() == null){
            menu.setParentId(0l);
        }
        if(menuService.editMenu(menu) <= 0){
            map.put("type", "error");
            map.put("msg", "修改失败，请联系管理员!");
            return map;
        }
        map.put("type","success");
        map.put("msg","修改成功！");
        return map;
    }
    /*菜单删除*/
    @RequestMapping(value = "/deleteMenu",method =RequestMethod.POST )
    @ResponseBody
    public Map<String,String> deleteMenu(Long id){
        Map<String, String> map = new HashMap<String, String>();
        if(id==null){
            map.put("type","error");
            map.put("msg","请选择要删除的菜单信息!");
            return map;
        }
        List<Menu> findChildernList = menuService.findChildernList(id);
        if(findChildernList != null && findChildernList.size() > 0){
            //表示该分类下存在子分类，不能删除
            map.put("type", "error");
            map.put("msg", "该分类下存在子分类，不能删除!");
            return map;
        }
        try {
            if (menuService.deleteMenu(id) <= 0) {
                map.put("type", "error");
                map.put("msg", "删除失败，请联系管理员!");
                return map;
            }
        }catch (Exception e){////authority表中添加了外键，menuId和menu表中的id对应，roleId和role表中的id对应
            //如果角色下存在权限或者用户信息，数据库无法删除，会报异常
            //这里捕捉异常，并提示错误
            map.put("type", "error");
            map.put("msg", "该菜单已被用户拥有权限，不能删除！");
            return map;
        }
        map.put("type","success");
        map.put("msg","删除成功！");
        return map;
    }
}
